package com.gipson.amc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsbj.common.msg.DataPageMessage;
import com.dsbj.common.msg.ResponseMessage;
import com.gipson.amc.entity.Company;
import com.gipson.amc.entity.PageByDataTable;
import com.gipson.amc.model.TreeNode;
import com.gipson.amc.service.basicdata.CompanyService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 公司管理 前端控制器
 * </p>
 *
 * @author xsyuan
 * @since 2020-05-09
 */
@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController {

    @Resource
    private CompanyService companyService;

    @RequestMapping("/list")
    @RequiresPermissions("company.list")
    public String list(){
        return "/basicdata/company/list";
    }
    
    /**
     * @description 详情页面
     * @param
     * @author: xsyuan 2020/6/8 14:26
     * @return
     */
    @RequestMapping("/detail")
    @RequiresPermissions("company.list")
    public String detail(Model model ,String id){
        Company company= companyService.getById(id==null?"":id);
        model.addAttribute("company",company);
        return "basicdata/company/detail";
    }
    
    @RequestMapping("/ajaxPageInfo")
    @RequiresPermissions("company.list")
    @ResponseBody
    public DataPageMessage ajaxPageInfo(Company company,PageByDataTable pageByDataTable){
        Page<Company> companyPage =companyService.getCompanyPage(company,pageByDataTable);
        return DataPageMessage.ok(companyPage.getRecords(),(int)companyPage.getTotal(),(int)companyPage.getTotal(),pageByDataTable.getSEcho(),"");
    }


    @RequestMapping("/edit")
    @RequiresPermissions(value = {"company.add","company.edit"},logical = Logical.OR)
    public String edit (Model model, String id){
        Company company= companyService.getById(id==null?"":id);
        model.addAttribute("company",company);
        return "/basicdata/company/edit";
    }

    @RequestMapping("/del")
    @RequiresPermissions("company.delete")
    @ResponseBody
    public ResponseMessage del (String ids){
        if(StringUtils.isNotBlank(ids)){
            companyService.delBatch(ids);
        }
        return ResponseMessage.ok("删除成功");
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"company.add","company.edit"},logical = Logical.OR)
    @ResponseBody
    public ResponseMessage saveOrUpdate( Company company){
        return  companyService.saveOrUpdateByCompany(company);
    }

    @RequestMapping("/findCompanyIdTree")
    @ResponseBody
    public ResponseMessage findCompanyIdTree(){
        List<TreeNode> treeList = new ArrayList<>();
        TreeNode rootTree = new TreeNode();
        rootTree.setId("0");
        rootTree.setPId("-1");
        rootTree.setIcon("/static/imgs/tree/1_open.png");
        rootTree.setType(0);
        rootTree.setName("公司");
        treeList.add(rootTree);
        QueryWrapper<Company> queryWrapper=new QueryWrapper<Company>().eq("DEL_FlAG",0);
        List<Company> companyList = companyService.list(queryWrapper);
        rootTree.setOpen(true);
        for (Company company:companyList){
            TreeNode treedictData = new TreeNode();
            treedictData.setId(company.getId());
            treedictData.setType(1);
            treedictData.setValue(company.getCompanyCode());
            treedictData.setPId(StringUtils.isBlank(company.getParentId())?rootTree.getId():company.getParentId());
            treedictData.setName(company.getCompanyName());
            treedictData.setIcon("/static/imgs/tree/company.png");
            treeList.add(treedictData);
        }
        return ResponseMessage.ok(treeList);
    }

    
}
